"""
Case Type   : 服务端工具
Case Name   : 验证gs_guc工具功能
Create At   : 2024.10.8
Owner       : wang-tianjing1
Description :
    1.显示帮助信息
    2.显示版本信息
Expect      :
    1.显示正确
    2.显示正确
History     :
"""
import unittest
import os
from yat.test import Node
from yat.test import macro
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger



class Tools(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.primary_node = Node('PrimaryDbUser')
        self.Constant = Constant()

    def test_server_tools1(self):
        self.log.info('----------step1:显示帮助信息;except:显示正确---------')
        cmd_list = ['-?', '--help']
        for cmd in cmd_list:
            check_cmd1 = f'''source {macro.DB_ENV_PATH}
                gs_guc {cmd}'''
            self.log.info(check_cmd1)
            msg1 = self.primary_node.sh(check_cmd1).result()
            self.log.info(msg1)

            self.log.info('--------------从Checking GUC parameters:开始截取msg1-------------------')
            self.log.info('-------------截取Checking GUC parameters:到Common options:部分的语法-------------------')
            start_index1 = msg1.find('Checking GUC parameters:') + len('Checking GUC parameters:')
            start_index2 = msg1.find('Common options:')
            temp = msg1[start_index1:start_index2].split('\n')
            options_list1 = []
            for j in temp:
                if j.lstrip().startswith('gs_guc'):
                    options_list1.append(j.strip())
                else:
                    pass
            else:
                pass
            self.log.info(options_list1)
            grammar = ['gs_guc check [-N NODE-NAME] {-I INSTANCE-NAME | -D DATADIR} {-c "parameter", -c "parameter", ...}',
                       'gs_guc check [-N NODE-NAME] {-I INSTANCE-NAME | -D DATADIR} {-c parameter, -c parameter, ...}',
                       'gs_guc {set | reload} [-N NODE-NAME] {-I INSTANCE-NAME | -D DATADIR} {-c "parameter = value" -c "parameter = value" ...}',
                       'gs_guc {set | reload} [-N NODE-NAME] {-I INSTANCE-NAME | -D DATADIR} {-c " parameter = value " -c " parameter = value " ...}',
                       'gs_guc {set | reload} [-N NODE-NAME] {-I INSTANCE-NAME | -D DATADIR} {-c "parameter = \'value\'" -c "parameter = \'value\'" ...}',
                       'gs_guc {set | reload} [-N NODE-NAME] {-I INSTANCE-NAME | -D DATADIR} {-c " parameter = \'value\' " -c " parameter = \'value\' " ...}',
                       'gs_guc {set | reload} [-N NODE-NAME] {-I INSTANCE-NAME | -D DATADIR} {-c "parameter" -c "parameter" ...}',
                       'gs_guc {set | reload} [-N NODE-NAME] {-I INSTANCE-NAME | -D DATADIR} -h "HOSTTYPE DATABASE USERNAME IPADDR IPMASK AUTHMEHOD authentication-options"',
                       'gs_guc {set | reload} [-N NODE-NAME] {-I INSTANCE-NAME | -D DATADIR} -h "HOSTTYPE DATABASE USERNAME IPADDR-WITH-IPMASK AUTHMEHOD authentication-options"',
                       'gs_guc {set | reload} [-N NODE-NAME] {-I INSTANCE-NAME | -D DATADIR} -h "HOSTTYPE DATABASE USERNAME HOSTNAME AUTHMEHOD authentication-options"',
                       'gs_guc {set | reload} [-N NODE-NAME] {-I INSTANCE-NAME | -D DATADIR} -h "HOSTTYPE DATABASE USERNAME IPADDR IPMASK"',
                       'gs_guc {set | reload} [-N NODE-NAME] {-I INSTANCE-NAME | -D DATADIR} -h "HOSTTYPE DATABASE USERNAME IPADDR-WITH-IPMASK "',
                       'gs_guc {set | reload} [-N NODE-NAME] {-I INSTANCE-NAME | -D DATADIR} -h "HOSTTYPE DATABASE USERNAME HOSTNAME"',
                       'gs_guc encrypt [-M keymode] -K password [-U username] -D DATADIR',
                       'gs_guc generate [-o prefix] -S cipherkey -D DATADIR']
            if len(options_list1) == len(grammar):
                for opt in options_list1:
                    if opt in grammar:
                        self.log.info(f'{opt}---------语法校验通过---------')
                    else:
                        self.log.error(f'{opt}---------语法校验不通过---------')
            else:
                self.log.error('---------语法校验有误---------')

            self.log.info('--------------定义一个空的列表，将获取的参数写入这个列表，并打印出该列表-------------------')
            options_list2 = []
            for i in msg1[start_index2:].split('\n'):
                for j in i.split(' '):
                    if len(j) != 0:
                        if j[0] == '-':
                            options_list2.append(j)
                        else:
                            pass
                    else:
                        pass
            self.log.info(options_list2)
            parameter = ['-N', '-I', '-D,', '--pgdata=DATADIR', '-c', '-c', '-h', '-?,', '--help', '-V,', '--version',
                         '-M,', '--keymode=MODE', '-K', '-U,', '--keyuser=USER', '-o', '-S']
            if len(options_list2) == len(parameter):
                for opt in options_list2:
                    if opt in parameter:
                        self.log.info(f'{opt}---------参数校验通过---------')
                    else:
                        self.log.error(f'{opt}---------参数校验不通过---------')
            else:
                self.log.error('---------参数校验有误---------')

        self.log.info('--------step2:显示版本号信息;except:显示正确------------------')
        cmd_list = ['-V', '--version']
        for cmd in cmd_list:
            check_cmd1 = f'''source {macro.DB_ENV_PATH}
                                gs_guc {cmd}'''
            self.log.info(check_cmd1)
            msg1 = self.primary_node.sh(check_cmd1).result()
            self.log.info(msg1)
            self.assertIn('openGauss', msg1)

    def tearDown(self):
        self.log.info('--------------无需清理环境-------------------')
        self.log.info(f'-----{os.path.basename(__file__)} end------')